package com.gljx.web.controller;

import com.gljx.constant.Constant;
import com.gljx.util.CommonUtil;
import com.gljx.util.ResourceUtil;
import com.gljx.web.Entity.Menu;
import com.gljx.web.Entity.User;
import com.gljx.web.service.MenuService;
import com.gljx.web.service.SysService;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class IndexAction {
	@Autowired
	HttpSession session;
	private  Logger logger = Logger.getLogger(getClass());
	@Autowired
	private SysService sysServcie;
	@Autowired
	private MenuService menuService;

	@RequestMapping("/index.action")
	public String Index(HttpServletRequest request){
		request.getSession().setAttribute("loadingFlag",0);
		//return "ui/glui";
		return "backHome/main";
	}
	
	//展示主页面
	@RequestMapping("/top.action")
	public ModelAndView showTop(){

		ModelAndView mav=new ModelAndView();
		mav.getModel().put("hasMessage",sysServcie.getMessageFlag());
		mav.getModel().put("name",SecurityUtils.getSubject().getPrincipal());
		mav.setViewName("backHome/top");
		return mav;
	}

	/**
	 * 获取 判断当前用户是否有消息的flag
	 * @Author:  LiXiang 
	 * @Date: Created in 2018/2/26 10:56
	 * @return
	 */
	@RequestMapping("/top/getmessage.action")
	@ResponseBody
	public boolean getMessage(){
		return sysServcie.getMessageFlag();
	}
	
	@RequestMapping("/left.action")
	public ModelAndView showLeft(){
		ModelAndView mav=new ModelAndView();
		mav.setViewName("backHome/left");
		return mav;
	}
	/**
	 * 获取菜单
	* @return
	* @author LiXiang  @date 2017年11月14日 下午4:08:54
	 */
	/*@RequestMapping("/left/getmenu.action")
	@ResponseBody
	public List<Power> getMenu(){
		Session session = SecurityUtils.getSubject().getSession();
		List<Power> powers = sysServcie.findTree((Integer)session
								.getAttribute(Constant.UserId_session), 
							Constant.Project_ID);
		return powers;
	}*/

	/**
	 * 获取当前用户菜单
	 * @Author:  LiXiang 
	 * @Date: Created in 2018/3/5 13:37
	 * @return
	 */
	@RequestMapping("/left/getUserMenu.action")
	@ResponseBody
	public List<Menu> gettsetMenu(){
		Session session = SecurityUtils.getSubject().getSession();
		List<Menu> menus = sysServcie.findUserMenuTree((Integer)session
						.getAttribute(Constant.UserId_session),
				Constant.Project_ID);
		return menus;
	}


	@RequestMapping("/bottom.action")
	public ModelAndView showBottom(){
		ModelAndView mav=new ModelAndView();
		Session session = SecurityUtils.getSubject().getSession();
		mav.getModel().put("CompanyName",session.getAttribute(Constant.CompanyName_session));
		mav.setViewName("backHome/bottom");
		return mav;
	}
	/**
	 * 首页右侧 菜单部分
	* @return
	* @throws IOException
	* @author LiXiang  @date 2017年11月13日 上午9:15:09
	 */
	@RequestMapping("/right.action")
	public ModelAndView showRight(HttpServletRequest request,String index) throws IOException{
		ModelAndView mav=new ModelAndView();
		Session session = SecurityUtils.getSubject().getSession();
		mav.getModel().put("indexOrg",sysServcie
				.selectCompanyByUser((Integer)SecurityUtils.getSubject()
										.getSession().getAttribute(Constant.UserId_session), 
									Integer.valueOf(ResourceUtil.getProper("system.indxcompany.num"))));
		mav.getModel().put("indexMenu",menuService.selectIndexTopMenu(Constant.Project_ID));
		mav.getModel().put("oftenMenu",menuService.selectIndexMenu());
		mav.getModel().put("CompanyName",session.getAttribute(Constant.CompanyName_session));
		mav.setViewName("backHome/right");
		if(("1").equals(index))
			session.setAttribute("loadingFlag",1);
		List<Menu> menus = sysServcie.findUserMenuTree((Integer)session
						.getAttribute(Constant.UserId_session),
				Constant.Project_ID);
		return mav;
	}
	
	/**
	 * 用户选定组织
	* @param companyID
	* @param companyName
	* @return
	* @throws MyBatisSystemException
	* @author LiXiang  @date 2017年11月14日 上午10:32:37
	 */
	@RequestMapping("/right/setCompany.action")
	@ResponseBody
	public Map <String,Object > setCompany(String companyID,String companyName,String flongnumber,
										   HttpServletRequest request) throws MyBatisSystemException{
		HttpSession sessions =  request.getSession();
		Session session = SecurityUtils.getSubject().getSession();

		//设置用户在session中的公司ID和公司名称。
		session.setAttribute(Constant.CompanyId_session,companyID);
		session.setAttribute(Constant.CompanyName_session, companyName);
		session.setAttribute(Constant.CompanyLFnumber_session, flongnumber);
		sysServcie.InsertCompanyUser(companyID, 
				 					CommonUtil.nowTime(),
									(Integer)session.getAttribute(Constant.UserId_session));
		//重置user中的公司等
		User user = (User) session.getAttribute("user");
		user.setFcompanyid(companyID);
		user.setCompanyLongFnumber(flongnumber);
		user.setCompanyName(companyName);
		sessions.setAttribute("user", user);
		Map <String,Object > dataMap = new HashMap<String, Object>();
		dataMap.put("success", "success");
		return dataMap;
	}
	
}
